﻿using Shumei.EFCore.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shumei.EFCore.SimpleService
{
    public class CourseService:BaseSimpleDAL<Course>
    {
        public CourseService()
        {

        }
        public Course Find(int id) 
        {

            Course res;
            try
            {
                res = Entities.Find(id);
            }
            catch
            {
                res = null;
            }
            return res;
        }
        /// <summary>
        /// 查找最新的
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public List<Course> FindAtLast(int num)
        {
            List<Course> res;
            try
            {
                res =Entities.OrderByDescending(x => x.UploadTime).Take(num).ToList();
            }
            catch
            {
                res = new List<Course>();
            }
            return res;
        }
        public List<Course> Find_Type(int type)
        {
            List<Course> res;
            try
            {
                res = Entities.Where(x => x.CourseType.Id == type).ToList();
            }
            catch
            {
                res = new List<Course>();
            }
            return res;
        }
        public List<Course> List()
        {
            List<Course> res;
            try
            {
                res = Entities.ToList();
            }
            catch
            {
                res = new List<Course>();
            }
            return Entities.ToList();
        }
        public List<Course> GetCoursesList(int page, out int totalPage, int num = 10)
        {
            List<Course> res;
            try
            {
                res = Entities.OrderBy(x => x.Id).Skip((page - 1) * 10).Take(num).ToList();
                totalPage = (Entities.Count() / num) + 1;
            }
            catch
            {
                res = new List<Course>();
                totalPage = 1;
            }
            return res;
        }
        public Course GetCourseDetail(int id)
        {
            try
            {
                return Entities.Find(id);
            }
            catch 
            {
                return null;
            }
        }
    }
}
